package com.jianli.smart.dao;

import java.sql.Connection;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DatabaseHelper {

	private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseHelper.class);
	
	public static void beginTranscation(){
		Connection connection = DBUtil.getConnection();
		if(connection != null){
			try {
				connection.setAutoCommit(false);
			} catch (SQLException e) {
				LOGGER.error("启动事务异常",e);
				throw new RuntimeException(e);
			}
		}
	}
	
	public static void commitTranscation(){
		Connection connection = DBUtil.getConnection();
		if(connection != null){
			try {
				connection.commit();
			} catch (SQLException e) {
				LOGGER.error("提交事务异常",e);
				throw new RuntimeException(e);
			} finally{
				DBUtil.closeConnection();
			}
		}
	}
	
	public static void rollBack(){
		Connection connection = DBUtil.getConnection();
		if(connection != null){
			try {
				connection.rollback();
			} catch (SQLException e) {
				LOGGER.error("提交事务异常",e);
			} finally{
				DBUtil.closeConnection();
			}
		}
	}
}
